<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import apiClient from '@/api.ts';
import { useSettingsStore } from '@/stores/settings';

interface Post {
  id: number;
  title: string;
  created_at: string;
}

const posts = ref<Post[]>([]);
const router = useRouter();
const settingsStore = useSettingsStore();

const cardStyle = computed(() => ({
  '--card-blur-intensity': `${settingsStore.cardBlurIntensity}px`,
}));

const fetchPosts = async () => {
  try {
    const response = await apiClient.get('/api/posts');
    posts.value = response.data;
  } catch (error) {
    console.error('Failed to fetch posts:', error);
  }
};

const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleDateString();
};

onMounted(fetchPosts);
</script>

<template>
  <div class="home-container">
    <header class="main-header">
      <h1>{{ settingsStore.siteTitle }}</h1>
      <nav>
        <RouterLink to="/">首页</RouterLink>
        <RouterLink to="/admin">管理后台</RouterLink>
      </nav>
    </header>
    <main class="content-area">
      <div class="post-list">
        <div v-if="posts.length === 0" class="no-posts">
          博主很懒，什么都还没写...
        </div>
        <div v-for="post in posts" :key="post.id" class="post-card" :style="cardStyle" @click="router.push(`/posts/${post.id}`)">
          <h2>{{ post.title }}</h2>
          <p class="post-date">发布于 {{ formatDate(post.created_at) }}</p>
        </div>
      </div>
    </main>
  </div>
</template>

<style scoped>
.home-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
  animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.main-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 2rem;
}

.main-header h1 {
  font-size: 2rem;
  margin: 0;
}

.main-header nav a {
  margin-left: 1.5rem;
  text-decoration: none;
  font-size: 1.1rem;
  color: var(--color-text);
  transition: color 0.2s;
}

.main-header nav a:hover {
  color: var(--color-primary);
}

.post-list {
  display: grid;
  gap: 1.5rem;
}

.post-card {
  background-color: rgba(255, 255, 255, 0.1); /* A light, semi-transparent base */
  backdrop-filter: blur(var(--card-blur-intensity));
  -webkit-backdrop-filter: blur(var(--card-blur-intensity));
  border: 1px solid var(--color-border);
  padding: 1.5rem;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.post-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.post-card h2 {
  margin: 0 0 0.5rem 0;
  font-size: 1.5rem;
}

.post-date {
  font-size: 0.9rem;
  color: var(--color-text-mute);
  margin: 0;
}

.no-posts {
  text-align: center;
  padding: 3rem;
  color: var(--color-text-mute);
}
</style>
